package com.bj58.analysis.tools.source;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import com.bj58.analysis.tools.common.bean.LogBean;
import com.bj58.analysis.tools.exception.InitDicException;

/**
 * @description 渠道解析工具
 * @author tzc
 *
 */
public class SourceParserUtils {
	// 是否初始化字典表标识
	private static boolean initDicFlag = false;

	/**
	 * @description 解析当前日志所属渠道
	 * @param bean
	 *            日志对象
	 * @param platformTag
	 *            平台标识,都为小写的,如pc\m\weixin\app
	 * @return 返回渠道列表，包含多级渠道,如果解析不正确均返回属于其它渠道
	 * @throws InitDicException
	 *             初始化字典表异常
	 */
	public static List<String> parser(LogBean bean,String platformTag)
			throws InitDicException {
		List<String> sourceList = null;
		if (initDicFlag) {
			if (bean != null) {
				sourceList = getSource(bean, platformTag);
			} else {
				sourceList = otherParser();
			}
		} else {// 初始化字典表异常
			throw new InitDicException();
		}
		return sourceList;
	}

	private static List<String> otherParser() {
		List<String> sourceList = new ArrayList<String>();
		sourceList.add("其他");
		sourceList.add("other");
		sourceList.add("other");
		return sourceList;
	}

	// listshow日志渠道解析
	private static List<String> getSource(LogBean bean, String platformTag) {
		List<String> sourceList = null;
		Map<String, String> map = getFields(bean);
		if (map != null && map.size() != 0) {
			SourceNameEntity sourceBean = new SourceNameEntity();
			// 平台
			if(StringUtils.isNotBlank(platformTag)){
				sourceBean.setPlatform(platformTag.toLowerCase());
			}
			// url
			if (map.containsKey("url")) {
				String url = map.get("url");
				sourceBean.setUrl(url);
			}
			// userAgent
			if (map.containsKey("userAgent")) {
				String userAgent = map.get("userAgent");
				sourceBean.setUserAgent(userAgent);
			}

			// pagetype
			if (map.containsKey("pageType")) {
				String pageType = map.get("pageType");
				sourceBean.setPageType(pageType);
			}

			// trackurl
			if (map.containsKey("trackUrl")) {
				String trackUrl = map.get("trackUrl");
				if (StringUtils.isNotBlank(trackUrl)) {
					try {
						JSONObject json = JSONObject.fromObject(trackUrl);
						if (json != null) {
							if (json.containsKey("utm_source")) {
								// utm_source
								String utm_source = json
										.getString("utm_source");
								if (StringUtils.isNotBlank(utm_source))
									sourceBean.setUtmSource(utm_source);
							}

							if (json.containsKey("spm")) {
								String spm = json.getString("spm");
								if (StringUtils.isNotBlank(spm))
									sourceBean.setMarketSpm(spm);
							}

							if (json.containsKey("init_refer")) {
								String init_refer = json
										.getString("init_refer");
								if (StringUtils.isNotBlank(init_refer))
									sourceBean.setInitRef(init_refer);
							}
						}
					} catch (Exception e) {

					}
				}
			}

			String sourceLine = PCMSourceFormat.getSourceName(sourceBean);
			if (StringUtils.isNotBlank(sourceLine)) {
				sourceList = new ArrayList<String>();
				String[] sources = sourceLine.split("\t", -1);
				for (String tmp : sources) {// 添加渠道
					sourceList.add(tmp);
				}
			}
		}
		return sourceList;
	}

	// 用反射获取所需字段
	private static Map<String, String> getFields(LogBean bean) {
		Map<String, String> map = null;
		if (bean != null) {
			map = new HashMap<String, String>();
			try {
				Class<? extends LogBean> claz = bean.getClass();
				String url = (String) claz.getMethod("getUrl").invoke(bean);
				if (StringUtils.isNotBlank(url)) {
					map.put("url", url);
				}

				String userAgent = (String) claz.getMethod("getUserAgent")
						.invoke(bean);
				if (StringUtils.isNotBlank(userAgent)) {
					map.put("userAgent", userAgent);
				}

				String pageType = (String) claz.getMethod("getPageType")
						.invoke(bean);
				if (StringUtils.isNotBlank(pageType)) {
					map.put("pageType", pageType);
				}

				String trackUrl = (String) claz.getMethod("getTrackUrl")
						.invoke(bean);
				if (StringUtils.isNotBlank(trackUrl)) {
					map.put("trackUrl", trackUrl);
				}
			} catch (Exception e) {

			}
		}
		return map;
	}

	/**
	 * @description 初始化字典资源
	 * @param spm
	 *            spm字典表
	 * @param pcSource
	 *            pc端渠道字典表
	 * @param mUnion
	 *            m端联盟字典表
	 * @param mSource
	 *            m端渠道字典表
	 */
	public static void init(List<String> spm, List<String> pcSource,
			List<String> mUnion, List<String> mSource) {
		PCMSourceFormat.initDict(spm, pcSource, mUnion, mSource);
		initDicFlag = true;
	}
}
